package teach.aistar.service.impl;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import teach.aistar.mapper.SeckillMapper;
import teach.aistar.service.ISeckillService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 本类用来演示:
 *
 * @author: success
 * @date: 2021/5/13 9:20 上午
 */
@Transactional
@Service
public class SeckillServiceImpl implements ISeckillService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private SeckillMapper seckillMapper;

    @Override
    public List<Map<String,Object>> unStartSeckill() {
        return seckillMapper.unStartSeckill();
    }

    @Override
    public int updateStarting() {
        return seckillMapper.updateStarting();
    }

    @Override
    public List<Long> findKillStarting() {
        return seckillMapper.findKillStarting();
    }

    @Override
    public int endKill() {
        return seckillMapper.endKill();
    }

    @Override
    public List<Long> findEndKillId() {
        return seckillMapper.findEndKillId();
    }

    @Override
    public String findStatus(Long secKillId) {
        ValueOperations vop = redisTemplate.opsForValue();
        String status = (String) vop.get("seckill_product_status_"+secKillId);
        if(null == status)
            status="0";//不能参与秒杀的状态
        return status;
    }

    @Override
    public void save2Rbm(String uName, Long secId,String email,Double prices) {
        Map<String,Object> dataMaps = new HashMap<>();

        dataMaps.put("uName",uName);
        dataMaps.put("secId",secId);
        dataMaps.put("email",email);
        dataMaps.put("prices",prices);


        System.out.println("111111111email"+email);

        rabbitTemplate.convertAndSend("j13_secKill","",dataMaps);
    }

    @Override
    public void saverRbm(String uName, Long secId,Double prices) {

        Map<String,Object> dataMaps = new HashMap<>();

        dataMaps.put("uName",uName);
        dataMaps.put("secId",secId);
        dataMaps.put("prices",prices);


        rabbitTemplate.convertAndSend("j131_secKill","",dataMaps);


    }


    @Override
    public int update(Long secId) {
       return seckillMapper.update(secId);
    }

}
